package com.wanmait.meteor.interceptor;

import com.wanmait.meteor.pojo.Manager;
import com.wanmait.meteor.service.ManagerService;
import com.wanmait.meteor.util.IgnoreToken;
import com.wanmait.meteor.util.JWTUtils;

import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
/*<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/manage/**"/>
<mvc:mapping path="/admin/**"/>
<mvc:exclude-mapping path="/manage/login"/>
<bean class="com.wanmait.mavendemo.interceptor.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>*/

//拦截器
@Component
public class JWTInterceptor implements HandlerInterceptor {

    @Resource
   private ManagerService managerService;


    //调用控制器的功能处理方法之前调用，返回true，会继续执行控制器的功能处理方法
    //返回false，不会执行控制器的功能处理方法
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle............");
        if(!(handler instanceof HandlerMethod)){
            return true;
        }
        //控制器方法
        HandlerMethod handlerMethod = (HandlerMethod)handler;
        Method method = handlerMethod.getMethod();
        //判断该方法上是否添加了@IgnoreToken注解
        if(method.isAnnotationPresent(IgnoreToken.class))
        {
            //获得控制器的方法上添加的注解
            IgnoreToken ignoreToken = method.getAnnotation(IgnoreToken.class);
            //@IgnoreToken(required = true)
            if(ignoreToken.required()){
                return true;
            }
        }
        //从请求头中获得token
        String token = request.getHeader("token");
        if(token==null){
            throw new Exception("没有登录");
        }
        //从token中获得管理员id
        Integer id = Integer.parseInt(JWTUtils.getAudience(token));
        Manager manager=managerService.findById(id);
        if(manager==null){
            throw new Exception("没有id为"+id+"的管理员");
        }
        //验证token是否有效
        JWTUtils.verifyToken(token,manager.getManagerPass());
        //拦截器和请求的控制器是一个请求，在控制器中可以直接使用admin
        request.setAttribute("manager",manager);
        return true;
    }

    //执行控制器的功能处理方法之后，视图渲染之前，可以对ModelAndView处理
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("状态代码"+response.getStatus());

        System.out.println("postHandle............");
    }

    //请求调用处理完成之后，视图渲染完毕，可以对资源进行清理
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("状态代码"+response.getStatus());
        //response.getWriter().print("404.................");
        System.out.println("afterCompletion............");
    }
}
